﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TD
{
    public delegate void MonsterDelegate(Monster monster);

    public class MonsterHelper : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game DefaultGame;
        public MonsterHelper(Game game)
            : base(game)
        {
            DefaultGame = game;
            Path = new List<Vector2>();
            Path.Add(new Vector2(100, 100));
            Path.Add(new Vector2(200, 300));
            Path.Add(new Vector2(500, 200));
            Path.Add(new Vector2(500, 500));

            StartPoint = Path[0];
            EndPoint = Path[Path.Count - 1];
        }

        public List<Monster> DeadMonsters = new List<Monster>();
        void PlayOver(Monster monster)
        {
            monster.DeadSeconds = TotalElapsedGameTime;
            DeadMonsters.Add(monster);
        }
        /// <summary>
        /// 怪物到达终点
        /// </summary>
        public event MonsterDelegate MonsterGoEnd;

        /// <summary>
        /// 绘制图形
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {

            TDGame.spriteBatch.Begin();
            foreach (var item in TDGame.Monsters)
            {

                TDGame.spriteBatch.Draw(defaultMonsterTexture,
                     new Rectangle((int)item.Vector.X, (int)item.Vector.Y, 30, 30)
                , Color.White);

            }
            TimeSpan limitTime = TotalElapsedGameTime.Add(new TimeSpan(0, 0, 0, 0, -5000));
            List<Monster> removes = new List<Monster>();
            foreach (var item in DeadMonsters)
            {
                if (item.DeadSeconds >= limitTime)
                {
                    TDGame.spriteBatch.Draw(deadMonsterTexture,
                       new Rectangle((int)item.Vector.X, (int)item.Vector.Y, 30, 30)
                  , Color.White);
                }
                else
                {
                    removes.Add(item);
                }
            }
            foreach (var item in removes)
            {
                DeadMonsters.Remove(item);
            }

           
            TDGame.spriteBatch.End();
        }

        /// <summary>
        /// 定时更新
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Update(GameTime gameTime)
        {
            if (!TDGame.IsPaused && !TDGame.IsLose)
            {
                //总流失时间
                TotalElapsedGameTime += gameTime.ElapsedGameTime;

                //是否要出新怪物
                if (ElapsedMonsterCount < MaxMonsterCount)
                {
                    if ((TotalElapsedGameTime.Seconds / 1) > ElapsedMonsterCount)//每隔5秒出一个怪
                    {
                        Monster monster = new Monster();
                        monster.Vector = Path[0];
                        monster.TargetVector = Path[1];
                        TDGame.Monsters.Add(monster);


                        ElapsedMonsterCount++;
                        //monster.
                    }
                }
            }
            if (!TDGame.IsPaused && !TDGame.IsLose)
            {
                List<Monster> removeMonster = new List<Monster>();
                //移动怪物
                foreach (var item in TDGame.Monsters)
                {
                    if (item.HP == 0)
                    {
                        removeMonster.Add(item);//这人挂了
                        PlayOver(item);
                        continue;
                        //结束了


                    }
                    if (item.CurrentPoint == Path.Count)//这人到终点
                    {
                        removeMonster.Add(item);
                        PlayOver(item);
                        continue;
                        //结束了
                    }
                    else
                    {
                        Vector2 next = Path[item.CurrentPoint];

                        if (next == item.Vector)
                        {
                            item.CurrentPoint++;
                            return;
                        }

                        Vector2 Change = next - item.Vector;
                        float L = (Change).Length();
                        if (L < 2)
                        {
                            item.CurrentPoint++;
                            return;
                        }


                        float XChange = Change.X * item.Speed / L;
                        float YChange = Change.Y * item.Speed / L;

                        item.Vector.X += XChange;
                        item.Vector.Y += YChange;

                    }
                }

                foreach (var item in removeMonster)
                {
                    if (MonsterGoEnd != null)
                    {
                        MonsterGoEnd(item);
                    }
                    TDGame.Monsters.Remove(item);
                }
            }
         
            base.Update(gameTime);


        }


        protected override void LoadContent()
        {
            deadMonsterTexture = DefaultGame.Content.Load<Texture2D>("Di/dead");
            defaultMonsterTexture = DefaultGame.Content.Load<Texture2D>("Di/di1");
            base.LoadContent();
        }

        #region 成员

        /// <summary>
        /// 本关卡已经进行的时间
        /// </summary>
        TimeSpan TotalElapsedGameTime;
        /// <summary>
        /// 本关卡 最大出怪数
        /// </summary>
        int MaxMonsterCount = 100;
        /// <summary>
        /// 本关卡 已经出怪的数目
        /// </summary>
        int ElapsedMonsterCount = 0;
        /// <summary>
        /// 本关卡 怪物路线路
        /// </summary>
        List<Vector2> Path;
        /// <summary>
        /// 默认的怪物图片
        /// </summary>
        Texture2D defaultMonsterTexture;

        Texture2D deadMonsterTexture;
        Vector2 StartPoint;
        Vector2 EndPoint;

        #endregion

    }
}